package com.subject.shuati.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.subject.shuati.common.enums.CategoryTypeEnum;
import com.subject.shuati.common.enums.IsDeleteFlagEnum;
import com.subject.shuati.domain.covert.SubjectLabelConverter;
import com.subject.shuati.domain.entity.SubjectLabelBo;
import com.subject.shuati.domain.service.SubjectLabelDomainService;
import com.subject.shuati.infra.basic.entity.SubjectCategory;
import com.subject.shuati.infra.basic.entity.SubjectLabel;
import com.subject.shuati.infra.basic.entity.SubjectMapping;
import com.subject.shuati.infra.basic.service.SubjectCategoryService;
import com.subject.shuati.infra.basic.service.SubjectLabelService;
import com.subject.shuati.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectCategoryService subjectCategoryService;
    @Override
    public Boolean addLabel(SubjectLabelBo subjectLabelBo) {
            if (log.isInfoEnabled()){
                log.info("SubjectLabelBoController.add.Bo:{}", JSON.toJSONString(subjectLabelBo));
            }
            SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.converterToLabelBo(subjectLabelBo);
            subjectLabel.setIsDeleted(IsDeleteFlagEnum.UN_DELETE.getCode());
            int count =  subjectLabelService.insert(subjectLabel);
            return count > 0;
    }

    @Override
    public Boolean update(SubjectLabelBo subjectLabelBo) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelBoController.update.Bo:{}", JSON.toJSONString(subjectLabelBo));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.converterToLabelBo(subjectLabelBo);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public Boolean delete(SubjectLabelBo subjectLabelBo) {
        if (log.isInfoEnabled()){
            log.info("subjectLabelController.delete.Bo:{}", JSON.toJSONString(subjectLabelBo));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.converterToLabelBo(subjectLabelBo);
        subjectLabel.setIsDeleted(IsDeleteFlagEnum.DELETE.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public List<SubjectLabelBo> queryLabelByCategoryId(SubjectLabelBo subjectLabelBo) {
        if (log.isInfoEnabled()){
            log.info("subjectLabelController.query.Bo:{}",JSON.toJSONString(subjectLabelBo));
        }
        //如果当前分类是1级分类，则查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBo.getCategoryId());
        if(CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()){
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBo.getCategoryId());
            List<SubjectLabel> labelList = subjectLabelService.queryByCondition(subjectLabel);
            List<SubjectLabelBo> labelResultList = SubjectLabelConverter.INSTANCE.converterToLabelBoList(labelList);
            return labelResultList;
        }
        Long categoryId = subjectLabelBo.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(Long.valueOf(categoryId));
        subjectMapping.setIsDeleted(IsDeleteFlagEnum.UN_DELETE.getCode());
        List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelByCategoryId(subjectMapping);
        if (CollectionUtils.isEmpty(subjectMappingList)){
            return Collections.emptyList();
        }
        List<Long> LabelIdList = subjectMappingList.stream().
                map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> subjectLabelList = subjectLabelService.queryBatchByLabelIds(LabelIdList);
        List<SubjectLabelBo> subjectLabelBoList = new ArrayList<>();
        subjectLabelList.forEach(subjectLabel -> {
            SubjectLabelBo bo = new SubjectLabelBo();
            bo.setId(subjectLabel.getId());
            bo.setLabelName(subjectLabel.getLabelName());
            bo.setSortNum(subjectLabel.getSortNum());
            bo.setCategoryId(categoryId);
            subjectLabelBoList.add(bo);
        });
        return subjectLabelBoList;
    }
}
